package com.cll.prototype.mongo.direct.demo;

import com.cll.prototype.mongo.direct.util.MongoClientUtil;
import com.mongodb.MongoClient;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.model.Filters;
import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;
import org.bson.Document;
import org.bson.types.ObjectId;

import java.util.Date;

/**
 * 描述信息:
 * 新增文档记录
 * @author CLL
 * @version 1.0
 * @date 2020/11/29 13:44
 */
public class DocumentDemo {

    public static void main(String[] args) {
//        insert();
//        update();
//        replace();
        delete();
        select();
    }

    public static void insert(){
        MongoClient clientInstance = MongoClientUtil.getClientInstance();
        MongoCollection<Document> collection =
                MongoClientUtil.getCollection(MongoClientUtil.getDatabase(clientInstance, ""), null);
        // 注意这里的时间。现在虽然插入的1993-01-01默认为0点，所以向前推进8个小时之后，数据库存储的就是1992-12-31 16:00:00
        Document document = Document.parse("{name: '王楠', birthday: new ISODate('1993-01-01'), city: '北京', gender: 0, expectSalary: 15000}");
        collection.insertOne(document);
        MongoClientUtil.close(clientInstance);
    }

    /**
     * 导致其它字段删除
     */
    public static void replace(){
        MongoClient clientInstance = MongoClientUtil.getClientInstance();
        MongoCollection<Document> collection =
                MongoClientUtil.getCollection(MongoClientUtil.getDatabase(clientInstance, ""), null);
        Document queryBson = new Document();
        queryBson.append("name", "陈林林");

        Document updateBson = new Document();
        updateBson.append("gender", 1);
        UpdateResult updateResult = collection.replaceOne(queryBson, updateBson);
        System.out.println(updateResult.toString());
        clientInstance.close();
    }

    /**
     * 修改的时候如果没有字段，会写入指定的字段
     */
    public static void update(){
        MongoClient clientInstance = MongoClientUtil.getClientInstance();
        MongoCollection<Document> collection =
                MongoClientUtil.getCollection(MongoClientUtil.getDatabase(clientInstance, ""), null);
        Document queryBson = new Document();
        queryBson.append("gender", 1);

        Document updateBson = new Document();
        updateBson.append("name", "陈林林");
        updateBson.append("city", "郑州");
        updateBson.append("gender", 0);
        updateBson.append("birthday", new Date());
        updateBson.append("expectSalary", 10000);


        Document updateBsonInfo = new Document();
        updateBsonInfo.append("$set", updateBson);
        UpdateResult updateResult = collection.updateOne(queryBson, updateBsonInfo);
        System.out.println(updateResult.toString());
        clientInstance.close();
    }

    public static void select(){
        MongoClient clientInstance = MongoClientUtil.getClientInstance();
        MongoCollection<Document> collection =
                MongoClientUtil.getCollection(MongoClientUtil.getDatabase(clientInstance, ""), null);
        Document queryBson = new Document();
        queryBson.append("gender", 0);

        Document sortBson = new Document();
        sortBson.append("expectSalary", -1);
        FindIterable<Document> findIterable = collection.find(queryBson).sort(sortBson);
        for (Document resume : findIterable) {
            System.out.println(resume);
        }

//        FindIterable<Document> expectSalary = collection.find(Filters.gt("expectSalary", 6000)).sort(sortBson);
//        for (Document resume : expectSalary) {
//            System.out.println(resume);
//        }
        clientInstance.close();
    }

    public static void delete(){
        MongoClient clientInstance = MongoClientUtil.getClientInstance();
        MongoCollection<Document> collection =
                MongoClientUtil.getCollection(MongoClientUtil.getDatabase(clientInstance, ""), null);

        Document deleteBson = new Document();
        deleteBson.append("_id", new ObjectId("5fc33276fe973605d8f47d1b"));
        DeleteResult deleteResult = collection.deleteOne(deleteBson);
        System.out.println("deleteResult = " + deleteResult.toString());

        clientInstance.close();
    }
}